<template>
  <div class="app-main">
    <el-card shadow="never">
      <el-row>
        <el-form :inline="true" :model="queryForm" ref="queryForm">
          <el-form-item label="服务名称" prop="name">
            <el-input v-model="queryForm.name" placeholder="请输入服务名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData">查询</el-button>
            <el-button @click="resetForm('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button type="primary" plain icon="el-icon-plus" @click="addShow">新增</el-button>
      </el-row>
      <el-row class="table-container">
        <el-table :data="tableData" v-loading="loading" element-loading-text="数据加载中···" stripe style="width: 100%" size="medium" max-height="630">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="服务名称" prop="name" show-overflow-tooltip></el-table-column>
          <el-table-column label="服务费用" prop="price" show-overflow-tooltip></el-table-column>
          <el-table-column label="服务说明" prop="remarks" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" class="el-icon-edit" @click="updateShow(scope.row)">修改</el-button>
              <el-button type="text" class="el-icon-delete" @click="deleteShow(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNo"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>
    <el-dialog :title="formTitle" :visible.sync="addFormVisible" width="600px" :close-on-click-modal="closeModal">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="90px">
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入服务名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="服务费用" prop="price">
          <el-input v-model="addForm.price" placeholder="请输入服务费用" clearable></el-input>
        </el-form-item>
        <el-form-item label="服务说明" prop="remarks">
          <el-input v-model="addForm.remarks" type="textarea" :rows="3" placeholder="请输入服务说明" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelForm('addForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addService, deleteService, getServiceList, updateService} from "@/api/service";

export default {
  name: "index",
  data() {
    return {
      queryForm: {
        name: ''
      },
      loading: false,
      tableData: [],
      page: {
        pageNo: 1,
        pageSizes: [10, 50, 100, 200, 500],
        pageSize: 10,
        total: 5
      },
      addFormVisible: false,
      formTitle: '新增',
      addForm: {
        id: '',
        name: '',
        price: '',
        remarks: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入服务名称', trigger: 'blur'},
        ],
        price: [
          {required: true, message: '请输入服务费用', trigger: 'blur'},
        ],
        remarks: [
          {required: true, message: '请输入服务说明', trigger: 'blur'},
        ],
      },
      closeModal: false
    }
  },
  created() {
    this.queryData()
  },
  methods: {
    queryData() {
      this.loading = true
      let params = {
        name: this.queryForm.name,
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize
      }
      getServiceList(params).then(response => {
        if(response.code === 200) {
          this.tableData = response.data.list
          this.page.total = response.data.total
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.queryData()
    },
    addShow() {
      this.formTitle = '新增'
      this.addForm.id = ''
      this.addForm.name = ''
      this.addForm.price = ''
      this.addForm.remarks = ''
      this.addFormVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.addForm.id !== '') {
            let params = {
              id: this.addForm.id,
              name: this.addForm.name,
              price: this.addForm.price,
              remarks: this.addForm.remarks,
            }
            updateService(params).then(response => {
              if(response.code === 200){
                this.$message({
                  message: response.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: response.msg,
                  type: 'error'
                });
              }
              this.addFormVisible = false
              this.queryData()
            })
          } else {
            let params = {
              name: this.addForm.name,
              price: this.addForm.price,
              remarks: this.addForm.remarks,
            }
            addService(params).then(response => {
              if(response.code === 200){
                this.$message({
                  message: response.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: response.msg,
                  type: 'error'
                });
              }
              this.addFormVisible = false
              this.queryData()
            })
          }
        } else {
          return false;
        }
      });
    },
    cancelForm(formName) {
      this.$refs[formName].resetFields();
      this.addFormVisible = false
    },
    updateShow(row) {
      this.formTitle = '修改'
      this.addForm.id = row.id
      this.addForm.name = row.name
      this.addForm.price = row.price
      this.addForm.remarks = row.remarks
      this.addFormVisible = true
    },
    deleteShow(row) {
      this.$confirm('是否确认删除选中的数据项？').then(function() {
        return deleteService(row.id)
      }).then((response) => {
        if(response.code === 200){
          this.$message({
            message: response.msg,
            type: 'success'
          });
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          });
        }
        this.queryData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.pageNo = 1
      this.queryData()
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.queryData()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  padding: 20px;
  box-sizing: border-box;

  .table-container {
    margin-top: 20px;
  }

  .pagination-container {
    margin: 20px 0;
    float: right;
  }
}
</style>

